#!/usr/bin/env Rscript
# Copyright (c) 2011-2012, Universite de Versailles St-Quentin-en-Yvelines
#
# This file is part of ASK.  ASK is free software: you can redistribute
# it and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation, version 2.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


suppressPackageStartupMessages(require(lhs, quietly=T))

# Parse the arguments
args <- commandArgs(trailingOnly = T)

if (length(args) != 3) {
    stop("Usage: latinsquare <configuration.conf> <input_file> <output_file>")
}

source(file.path(Sys.getenv("ASKHOME"), "common/configuration.R"))

# Check that n is an integer
if (!is.numeric(conf("modules.sampler.params.n"))) {
  stop("modules.sampler.params.n must be an integer")
}
n = conf("modules.sampler.params.n")

# load previous latinsquare non resampled data : "samples"
load(file.path(conf("output_directory"), "last_latinsquare.data"))

# augment it
samples = augmentLHS(samples, n)

# Save latinsquare model for possible future augmenting by another latinsquare
# sampler
save(samples, file=file.path(conf("output_directory"), "last_latinsquare.data"))

# keep only the new samples, which are the last n lines of the
# samples matrix
samples = samples[(nrow(samples)-n+1):(nrow(samples)),]

# Resample columns according to the factor ranges
# to get something close to the original [0, 1] lhs model.
i = 1
for (f in conf("factors")) {
  if (f$type == "categorical") {
    samples[,i] = length(f$values)*samples[,i]
  }
  else
  {
    samples[,i] = f$range$min + (f$range$max-f$range$min)*samples[,i]
  }

  # Round columns for non float factors
  if (f$type != "float") {
    samples[,i] = as.integer(samples[,i])
  }
  i = i + 1
}

# Write samples to output file
write.table(samples, file=args[3], sep=" ", quote=F, col.names=F, row.names=F)
